package com.imooc.miaosha.controller;

import com.imooc.miaosha.domian.User;
import com.imooc.miaosha.rabbitmq.MqSender;
import com.imooc.miaosha.redis.RedisFactory;
import com.imooc.miaosha.redis.keys.UserKey;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.result.ResultEnums;
import com.imooc.miaosha.service.UserService;
import com.imooc.miaosha.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 模块描述: <br>
 * ()
 *
 * @Author: Mr. xyb
 * @Date: 2019/4/13 16:43
 * @since: 1.8.0
 * @version: 1.0.0
 */
@Controller
public class SimpleController {

    @RequestMapping("/hello")
    public String hello(Model model) {
        System.out.println("aa");
        model.addAttribute("name", "xyb");
        return "hello";
    }

    @RequestMapping("/success")
    @ResponseBody
    public Result<Object> success() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "xyub");
        String s = Md5Utils.inputPassToDbPass("123456", "1234567");
        map.put("s", s);
        return Result.success(map, ResultEnums.SUCCESS.getMsg());
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public Result<User> getUser() {
        User user = userService.getUserById(1L);
        return Result.success(user, ResultEnums.SUCCESS.getMsg());
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public Result<Boolean> addUser() {
        userService.addUser();
        return Result.success(true);
    }

    @RequestMapping("/redisGet")
    @ResponseBody
    public Result<Long> redisGet() {
        Long v1 = redisFactory.get(UserKey.getById, "1", Long.class);
        return Result.success(v1);
    }

    @RequestMapping("/redisSet")
    @ResponseBody
    public Result<Boolean> redisSet() {
        UserKey userKey = UserKey.getById;
        userKey.setExpireSeconds(5);
        boolean flag = redisFactory.set(userKey, "1", "1234561");
        return Result.success(flag);
    }

    @RequestMapping("/mq")
    @ResponseBody
    public Result<Boolean> rabbitMq() {
        mqSender.send("Direct模式");
        mqSender.topicSend("topic msg");
        mqSender.sendHeader("headers msg");
        return Result.success(true);
    }

    @Autowired
    private MqSender mqSender;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisFactory redisFactory;

}
